#include "pop3studialog.h"
#include "02-socket/02-pop3/ui_pop3studialog.h"

Pop3StuDialog::Pop3StuDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Pop3StuDialog)
{
    ui->setupUi(this);
}

Pop3StuDialog::~Pop3StuDialog()
{
    delete ui;
}

void Pop3StuDialog::onSocketReceiveData() {
    QByteArray buffer;
    //读取缓冲区数据
    buffer = this->mSocket->readAll();
    QString str = "";
    if(!buffer.isEmpty()){
        str+= buffer;
    }
    this->ui->textEdit->setText(str);
    qDebug()<<"there is msg from server:" << str;
}



void Pop3StuDialog::on_pushButton_3_clicked()
{
    this->mSocket->disconnectFromHost();
    this->mSocket->close();
}


void Pop3StuDialog::on_pushButton_4_clicked()
{

}

void Pop3StuDialog::sendCmd(QString cmd) {
     this->ui->te_client->setText(cmd);
    this->mSocket->write(cmd.toUtf8().constData());
    this->ui->te_client->setText(cmd);
    //发送.
    this->mSocket->flush();
}


void Pop3StuDialog::on_pushButton_5_clicked()
{
    QString cmd = "A01 LIST \"\" \"*\"\r\n";
    this->sendCmd(cmd);
}


/**
 * @brief ImapStuDialog::on_btn_sendCmd_clicked
 * 发送自定义命令
 * 程序帮助用户增加\r\n.
 */
void Pop3StuDialog::on_btn_sendCmd_clicked()
{
    QString str = this->ui->te_cmd->toPlainText();
    if(str.length() > 0) {
        str+="\r\n";
        this->sendCmd(str);
    }
}


void Pop3StuDialog::on_pushButton_8_clicked()
{
    QString cmd = "A02 SELECT INBOX\r\n";
    this->sendCmd(cmd);
}

/**
 * 查看当前文件夹的所有邮件的index
 * @brief ImapStuDialog::on_pushButton_9_clicked
 */
void Pop3StuDialog::on_pushButton_9_clicked()
{
    QString cmd = "A03 SEARCH ALL\r\n";
    this->sendCmd(cmd);
}



/**
 * 查看邮件UID
 * @brief ImapStuDialog::on_pushButton_7_clicked
 */
void Pop3StuDialog::on_pushButton_7_clicked()
{
    QString cmd = "A03 FETCH 1 UID\r\n";
    this->sendCmd(cmd);
}

/**
 * 查看邮件subject
 * @brief ImapStuDialog::on_pushButton_10_clicked
 */
void Pop3StuDialog::on_pushButton_10_clicked()
{
    QString cmd = "A03 FETCH 1 BODY[HEADER]\r\n";
    this->sendCmd(cmd);
}


/**
 * 注销
 * @brief ImapStuDialog::on_pushButton_11_clicked
 */
void Pop3StuDialog::on_pushButton_11_clicked()
{
    QString cmd = "A03 LOGOUT\r\n";
    this->sendCmd(cmd);
}


void Pop3StuDialog::on_pushButton_12_clicked()
{
    QString cmd = "A03 FETCH 1 (UID BODY[HEADER])\r\n";
    this->sendCmd(cmd);
}

/**
 * 获取ENVELOPE
 * @brief ImapStuDialog::on_pushButton_13_clicked
 */
void Pop3StuDialog::on_pushButton_13_clicked()
{
    QString cmd = "A03 FETCH 1 (ENVELOPE)\r\n";
    this->sendCmd(cmd);
}


/**
 * 输入用户名信息
 * @brief Pop3StuDialog::on_btn_user_clicked
 */
void Pop3StuDialog::on_btn_user_clicked()
{
    QString cmd = "";
    cmd += "USER " + this->user + "\r\n";
    this->sendCmd(cmd);
}

/**
 * 输入密码信息
 * @brief Pop3StuDialog::on_btn_user_clicked
 */
void Pop3StuDialog::on_btn_pass_clicked()
{
    QString cmd = "";
    cmd += "PASS " + this->pass + "\r\n";
    this->sendCmd(cmd);
}


void Pop3StuDialog::on_btn_connect_clicked()
{
    this->mSocket = new QTcpSocket(this);
    QString serverIp1 = "172.16.10.30";
    QString serverIp2 = "172.16.10.110";
    QString serverIp = serverIp2;
    int port = defaultPort;
    this->mSocket->connectToHost(serverIp,port);
    QString info = "connect " + serverIp + " : " + QString::number(port);
    this->ui->te_client->setText(info);
    connect(this->mSocket, &QTcpSocket::readyRead, this, &Pop3StuDialog::onSocketReceiveData);

}

/**
 * 查看邮件index列表
 * @brief Pop3StuDialog::on_btn_mailIndexList_clicked
 */
void Pop3StuDialog::on_btn_mailIndexList_clicked()
{
    QString cmd = "LIST\r\n";
    this->sendCmd(cmd);
}


void Pop3StuDialog::on_btn_mailUidList_clicked()
{
    QString cmd = "UIDL\r\n";
    this->sendCmd(cmd);
}


void Pop3StuDialog::on_btn_stat_clicked()
{
    QString cmd = "STAT\r\n";
    this->sendCmd(cmd);
}


void Pop3StuDialog::on_btn_fetchEml_clicked()
{
    QString cmd = "RETR 1\r\n";
    this->sendCmd(cmd);
}


void Pop3StuDialog::on_btn_getMailInfo_clicked()
{
    QString cmd = "LIST 1\r\n";
    this->sendCmd(cmd);
}


void Pop3StuDialog::on_btn_getMailTop_clicked()
{
    QString cmd = "TOP 1 100\r\n";
    this->sendCmd(cmd);
}


void Pop3StuDialog::on_btn_getMailUid_clicked()
{
    QString cmd = "UIDL 1\r\n";
    this->sendCmd(cmd);
}


void Pop3StuDialog::on_btn_noop_clicked()
{
    QString cmd = "NOOP\r\n";
    this->sendCmd(cmd);
}


void Pop3StuDialog::on_btn_deleteMail_clicked()
{
    QString cmd = "DELE 1\r\n";
    this->sendCmd(cmd);
}


void Pop3StuDialog::on_btn_quit_clicked()
{
    QString cmd = "QUIT\r\n";
    this->sendCmd(cmd);
}

